package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.*;
import com.itheima.utils.DateUtils;
import org.aspectj.weaver.ast.Or;
import com.itheima.pojo.OrderSettingList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

/**
 * @author houzongtao
 * @date 2020/8/30 16:07
 */
@Service(interfaceClass = OrderSettingListService.class)
@Transactional
public class OrderSettingListServiceImpl implements OrderSettingListService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private SetmealDao setmealDao;

    //分页展示预约信息
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<OrderSettingList> page = orderDao.selectByCondition(queryPageBean);
        return new PageResult(page.getTotal(), page.getResult());
    }

    //电话新增预约
    public Result add(OrderSettingList orderSettingList, Integer setmealId) throws Exception {
        //1.检查当前日期是否进行了预约设置
        String orderDate = orderSettingList.getOrderDate();
        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);
        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //2.检查预约日期是否预约已满
        int number = orderSetting.getNumber();//可预约人数
        int reservations = orderSetting.getReservations();//已预约人数
        if (reservations >= number) {
            //预约已满，不能预约
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //3.检查用户是否已经预约过相同的套餐(姓名，手机号，预约日期，预约套餐 )
        String setmealName = setmealDao.findsetmealNameById(setmealId);
        orderSettingList.setSetmealName(setmealName);
        OrderSettingList ost = orderDao.findByOSL(orderSettingList);
        if (ost != null) {
            return new Result(false, MessageConstant.HAS_ORDERED);
        }
        //可以预约，设置预约人数加一
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);

        Integer memberId = memberDao.findMenberIdByPhoneNumber(orderSettingList.getPhoneNumber());
        Order order = new Order(memberId, date, Order.ORDERTYPE_TELEPHONE, Order.ORDERSTATUS_NO, setmealId);
        orderDao.add(order);
        return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());
    }

    //编辑预约数据回显
    public OrderSettingList findById(Integer id) {
        return orderDao.findOrderListById(id);
    }

    //编辑预约套餐回显
    public Integer findSetmealIdByOrderId(Integer id) {
        return orderDao.findSetmealIdByOrderId(id);
    }

    //编辑
    public Result edit(OrderSettingList orderSettingList, Integer setmealId) throws Exception {
        //1.检查当前日期是否进行了预约设置
        String orderDate = orderSettingList.getOrderDate();
        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);
        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //2.检查预约日期是否预约已满
        int number = orderSetting.getNumber();//可预约人数
        int reservations = orderSetting.getReservations();//已预约人数
        if (reservations >= number) {
            //预约已满，不能预约
            return new Result(false, MessageConstant.ORDER_FULL);
        }

        Integer id = orderSettingList.getId();
        String orderDataDB = orderDao.findDateById(id);//可能需要强转
        if (!orderSettingList.getOrderDate().equals(orderDataDB)) {
            //可以修改预约，当前日期预约数-1，修改的日期预约数+1
            OrderSetting orderSettingOld = orderSettingDao.findByOrderDate(DateUtils.parseString2Date(orderDataDB));
            orderSettingOld.setReservations(orderSetting.getReservations() - 1);
            orderSettingDao.editReservationsByOrderDate(orderSetting);
            orderSetting.setReservations(orderSetting.getReservations() + 1);
            orderSettingDao.editReservationsByOrderDate(orderSetting);
        }
        Integer memberId = memberDao.findMenberIdByPhoneNumber(orderSettingList.getPhoneNumber());
        Order order = new
                Order(
                orderSettingList.getId(),
                memberId,
                DateUtils.parseString2Date(orderSettingList.getOrderDate()),
                orderSettingList.getOrderType(),
                orderSettingList.getOrderStatus(),
                setmealId
        );
        orderDao.edit(order);
        Member member = orderDao.findMemberByOrderId(orderSettingList.getId());
        member.setName(orderSettingList.getMemberName());
        memberDao.edit(member);
        return new Result(true ,MessageConstant.EDIT_ORDER_SUCCESS);
    }


    //删除预约
    public void delete(Integer id) {
        orderDao.deleteOrder(id);
    }

    //修改预约状态
    public void changeStatus(Map map) {
        orderDao.editStatusByorderId(map);
    }

}
